package com.air.antispider.stream.dataprocess.businessprocess

import java.util.regex.{Matcher, Pattern}

import com.air.antispider.stream.dataprocess.constants.TravelTypeEnum
import com.air.antispider.stream.dataprocess.constants.TravelTypeEnum.TravelTypeEnum

/**
  * 往返信息打标签
  */
object TravelTypeClassifier {
  def travelTypeClassifier(httpReferrer: String): TravelTypeEnum = {
    //看当前httpReferrer里面包含几个日期
//https://b2c.csair.com/B2C40/newTrips/static/main/page/booking/index.html
    // ?t=R&c1=CTU&c2=DLC&d1=2019-08-01&d2=2019-09-25&at=1&ct=0&it=0
    //1. 直接切割字符串,查找d1/d2的日期个数
    //2. 使用正则,进行匹配,看匹配几个
    val pattern: Pattern = Pattern.compile("(\\d{4})-(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])")
    //开始匹配
    val matcher: Matcher = pattern.matcher(httpReferrer)
    //匹配的次数,默认为0
    var count = 0
    //看匹配了几次
    while (matcher.find()) {
      count = count + 1
    }

    if (count == 1) {
      //单程
      return TravelTypeEnum.OneWay
    } else if (count == 2) {
      //往返
      return  TravelTypeEnum.RoundTrip
    } else {
      //不知道啊
      return  TravelTypeEnum.Unknown
    }


  }

}
